﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

using FCNS.KeepMoneyLibrary.Util;

namespace FCNS.KeepMoneyLibrary.Data
{
    public class RegularEntryField
    {
        public void CalcRegular()
        {
            if (Finish == columnTime)
                return;

            BankField bank = BankField.GetBankField.Find(bf => { return bf.Id == BankId; });
            Debug.Assert(bank != null);
            InterestField interest = InterestField.GetInterestField.Find(gf => { return gf.Id == InterestId; });
            Debug.Assert(interest != null);

            int usableTime = CalcUsableTime(interest);
            for (int i = 1; i <= usableTime; i++)
            {
                Finish = Finish + 1;

                EntryField entryField = new EntryField(GroupId);
                entryField.columnName = columnName;
                entryField.BankId = BankId;
                entryField.columnNote = Finish.ToString() +"期";//alert
                switch (interest.Mode)
                {
                    case 0:
                        entryField.Date = columnStartDate.AddDays(Finish);
                        break;
                    case 1:
                        entryField.Date = columnStartDate.AddDays(Finish*7);
                        break;
                    case 2:
                        entryField.Date = columnStartDate.AddMonths(Finish);
                        break;
                    case 3:
                        entryField.Date = columnStartDate.AddYears(Finish);
                        break;
                }
                entryField.Money =MoneyPerTime;//别用变量的 moneyPerTime ; 因为它有可能还没有计算的
                Debug.Assert(entryField.Money != 0);

                entryField.EntryType = KMDEntryType.Regular;
                entryField.Mark = Id.ToString();
                entryField.AddRowToDataTable();

                bank.SetBankBalance(entryField.Money);
            }
        }

        decimal moneyPerTime = 0;
        public decimal MoneyPerTime
        {
            get
            {
                if (moneyPerTime == 0)
                    CalcMoneyPerTime();

                return moneyPerTime;
            }
        }

        private int CalcUsableTime(InterestField interest )
        {
            int allTime = 0;
            switch (interest.Mode)
            {
                case 0:
                    allTime = DateTime.Now.DayOfYear - columnStartDate.DayOfYear;
                    for (int i = columnStartDate.Year; i < DateTime.Now.Year; i++)
                        allTime += (i % 4 == 0) ? 366 : 365;

                    break;

                case 1:
                    allTime = DateTime.Now.DayOfYear - columnStartDate.DayOfYear;
                    for (int i = columnStartDate.Year; i < DateTime.Now.Year; i++)
                        allTime += (i % 4 == 0) ? 366 : 365;

                    allTime = allTime / 7;
                    break;

                case 2:
                    allTime = DateTime.Now.Month - columnStartDate.Month;
                    for (int i = columnStartDate.Year; i < DateTime.Now.Year; i++)
                        allTime += 12;

                    if (DateTime.Now.Day < columnStartDate.Day)
                        allTime -= 1;

                    break;

                case 3:
                    allTime = DateTime.Now.DayOfYear / columnStartDate.DayOfYear;
                    break;
            }

            if (allTime > columnTime)
                allTime = columnTime;

            allTime -= Finish;

            return allTime;
        }
        /// <summary>
        /// 每期的钱与息
        /// </summary>
        private void CalcMoneyPerTime()
        {
            InterestField interest = InterestField.GetInterestField.Find(gif => { return gif.Id == InterestId; });
            Debug.Assert(interest != null);

            moneyPerTime = 0;

            if (interest.Value > 1)
                moneyPerTime = interest.Value;
            else
                moneyPerTime = columnMoney * interest.Value;


            moneyPerTime += Math.Round(columnMoney / columnTime,LibDefines.decimalDigits);
        
        }


        static List<RegularEntryField> fields = new List<RegularEntryField>();
        public static List<RegularEntryField> GetRegularEntryField
        {
            get
            {
                if (fields.Count == 0)
                    foreach (DataRow row in KMData.DataTableRegular.Rows)
                    {
                        RegularEntryField r = new RegularEntryField(row);
                        fields.Add(r);
                    }
                return fields;
            }
        }

        public void Remove()
        {
            foreach (EntryField ef in EntryField.GetEntryField.FindAll(ef => { return ef.Mark == Id.ToString(); }))
                ef.Remove();

            KMData.DataTableRegular.Rows.Remove(dRow);
            fields.Remove(this);
        }

        bool add = false;
        public void AddRowToDataTable()
        {
            if (!add)
                return;

            KMData.DataTableRegular.Rows.Add(dRow);
            fields.Add(this);
            add = false;
        }

        DataRow dRow = null;
        public RegularEntryField(DataRow row)
        {
            if (row == null)
            {
                dRow = KMData.DataTableRegular.NewRow();
                add = true;
            }
            else
                dRow = row;
        }


        public int Id
        {
            get { return (int)dRow["Id"]; }
        }

        public string columnNote
        {
            get { return (string)dRow["Note"]; }
            //set { dRow["Note"] = value; }
        }
        /// <summary>
        /// 已完成期数
        /// </summary>
        public int Finish
        {
            get { return (int)dRow["Finish"]; }
            set { dRow["Finish"] = value; }
        }

        public string columnFinish2
        {
            get { return dRow["Finish"].ToString() + "/" + dRow["Time"].ToString(); }
        }

        public int GroupId
        {
            get { return (int)dRow["GroupId"]; }
            set { dRow["GroupId"] = value; }
        }

        public string columnGroupName
        {
            get
            {
                return GroupField.GetGroupField.Find(gf => { return gf.Id == GroupId; }).Name;}
        }

        public int BankId
        {
            get { return (int)dRow["BankId"]; }
            set { dRow["BankId"] = value; }
        }

        public string columnBankName
        {
            get
            {
                return BankField.GetBankField.Find(bf =>
                { return bf.Id == (int)dRow["BankId"]; }).Name;
            }
        }

        public DateTime columnStartDate
        {
            get { return (DateTime)dRow["StartDate"]; }
            set { dRow["StartDate"] = value; }
        }

        public string columnName
        {
            get { return (string)dRow["Name"]; }
            set { dRow["Name"] = value; }
        }

        public decimal columnMoney
        {
            get { return (decimal)dRow["Money"]; }
            set { dRow["Money"] = value;
            moneyPerTime = 0;
            }
        }
        /// <summary>
        /// 账单日
        /// </summary>
        public int columnDay
        {
            get { return (int)dRow["Day"]; }
            set { dRow["Day"] = value; }
        }
        /// <summary>
        /// 期数
        /// </summary>
        public int columnTime
        {
            get { return (int)dRow["Time"]; }
            set { dRow["Time"] = value; }
        }

        public int InterestId
        {
            get { return (int)dRow["InterestId"]; }
            set { dRow["InterestId"] = value;
            moneyPerTime = 0;
            }
        }

        public string columnInterestName
        {
            get { return (string)KMData.DataTableInterest.Select("Id = " + InterestId)[0]["Name"]; }
        }

        public bool columnUseCredit
        {
            get { return (bool)dRow["UseCredit"]; }
            set { dRow["UseCredit"] = value; }
        }

        public bool columnCompound
        {
            get { return (bool)dRow["Compound"]; }
            set { dRow["Compound"] = value; }
        }
    }
}